package com.phkj.qdh.base

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update

abstract class BaseVM:ViewModel() {
    private val _uiState = MutableStateFlow(STATE_NORMAL)
    val uiState: StateFlow<Int> get() = _uiState

    //开始加载
    fun startLoading() {
        _uiState.update {STATE_LOADING  }
    }

    //加载完毕
    fun stopLoading() {
        _uiState.update{STATE_NORMAL}
    }

    //加载失败
    fun loadFail() {
        _uiState.update { STATE_FAIL }
    }

    fun loadSuccess() {
        _uiState.update { STATE_SUCCESS }
    }

    companion object {
        const val STATE_NORMAL = 0//无数据请求状态
        const val STATE_LOADING = 1//开始加载
        const val STATE_FAIL = 2//加载失败
        const val STATE_SUCCESS = 3//加载成功
    }

}
